package cn.hchaojie.camp.javafx.part1;

import java.io.IOException;
import java.net.URL;

import cn.hchaojie.camp.javafx.part1.Palette2Controller.OnColorHandler;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class RectangleScene extends Application implements OnColorHandler {
	private Rectangle rectangle;

	@Override
	public void start(Stage stage) throws Exception {
		Scene scene = new Scene(createContents());
		
		stage.setScene(scene);
		stage.show();
	}
	
	// 在这里创建页面布局
	private Parent createContents() {
		VBox box = new VBox(10);
		box.setPadding(new Insets(10));
		
		rectangle = new Rectangle(200, 100);
		rectangle.setFill(Color.RED);
		box.getChildren().add(rectangle);
		
		Button button = new Button("设置颜色");
		button.setOnAction((event) -> {
			// 创建一个窗口，用户可以设置颜色
			Stage stage = new Stage();
			

			URL url = this.getClass().getResource("pallete2.fxml");
			VBox root = null;
			try {
				FXMLLoader loader = new FXMLLoader(url);
				root = loader.load();
				
				Palette2Controller controller = loader.getController();
				controller.setOnColorHandler(RectangleScene.this);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			Scene scene = new Scene(root);
			stage.setScene(scene);

			stage.show();
		});
		
		box.getChildren().add(button);

		return box;
	}
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void handleColor(Color color) {
		rectangle.setFill(color);
	}
}